package com.example.certificate.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.annoatiton.CurrentUser;
import com.example.certificate.annoatiton.LoginToken;
import com.example.certificate.dto.BasePageDTO;
import com.example.certificate.dto.InsProductDto;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.entity.DataType;
import com.example.certificate.entity.ProductInfo;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.DataTypeMapper;
import com.example.certificate.mapper.ProductInfoMapper;
import com.example.certificate.utils.ResultEntity;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao
 * @since 2022-03-10 12:42:47
 */
@RestController
@RequestMapping("/domain/productInfo")
public class ProductInfoController {

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private DataTypeMapper dataTypeMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "查询商品详情")
    @PostMapping(value = "/productDesc")
    public ResultEntity productDesc(@Validated(value = {DelByIdDto.delDataById.class}) @RequestBody DelByIdDto dto){
        redisTemplate.opsForZSet().add("detail", dto.getId(), 1);
        return new ResultEntity(productInfoMapper.selectById(dto.getId()));
    }


    @LoginToken
    @ApiOperation(value = "分页查询商品")
    @PostMapping(value = "/selProductPage")
    public ResultEntity selProductPage(@RequestBody BasePageDTO dto, @Ignore @CurrentUser User user){
        Page<ProductInfo> page = new Page<>();
        page.setCurrent(dto.getCurrent() == null ? 1 : dto.getCurrent());
        page.setPages(dto.getPageSize() == null ? 10 : dto.getPageSize());

        if(user.getRoleId().equals("1703675027755974657")){
            page = productInfoMapper.pagesel(page, dto, null);
        }else {
            page = productInfoMapper.pagesel(page, dto, user.getId());
        }

        if(!CollectionUtils.isEmpty(page.getRecords())){
            page.getRecords().forEach(item ->{
                DataType dataType = dataTypeMapper.selectById(item.getBusinessId());
                if(dataType != null)
                    item.setBusinessVal(dataType.getType());
            });
        }
        return new ResultEntity(page);
    }


    @ApiOperation(value = "分页查询商品")
    @PostMapping(value = "/selProductPages")
    public ResultEntity selProductPages(@RequestBody BasePageDTO dto){
        Page<ProductInfo> page = new Page<>();
        page.setCurrent(dto.getCurrent() == null ? 1 : dto.getCurrent());
        page.setPages(dto.getPageSize() == null ? 10 : dto.getPageSize());

        Page<ProductInfo> pagesel = productInfoMapper.pagesel(page, dto, null);
        if(!CollectionUtils.isEmpty(pagesel.getRecords())){
            pagesel.getRecords().forEach(item ->{
                DataType dataType = dataTypeMapper.selectById(item.getBusinessId());
                if(dataType != null)
                    item.setBusinessVal(dataType.getType());
            });
        }
        return new ResultEntity(pagesel);
    }


    @LoginToken
    @ApiOperation(value = "删除商品")
    @PostMapping(value = "/delProduct")
    public ResultEntity delProduct(@Validated(value = {DelByIdDto.delDataById.class}) @RequestBody DelByIdDto dto){
        return new ResultEntity(productInfoMapper.deleteById(dto.getId()));
    }


    @LoginToken
    @ApiOperation(value = "新增作品")
    @PostMapping(value = "/insProduct")
    public ResultEntity insProduct(@RequestBody InsProductDto dto, @CurrentUser @Ignore User user){
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(dto,productInfo);
        productInfo.setUserId(user.getId());
        return new ResultEntity(productInfoMapper.insert(productInfo));
    }


    @LoginToken
    @ApiOperation(value = "编辑作品")
    @PostMapping(value = "/editProduct")
    public ResultEntity editProduct(@RequestBody ProductInfo model){
        return new ResultEntity(productInfoMapper.updateById(model));
    }
}

